//
//  CLReadView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/8/13.
//

#import "CLReadView.h"

#import "CLCoreText.h"
@implementation CLReadView

- (void)setPageModel:(CLReadPageModel *)pageModel {
    _pageModel = pageModel;
    self.frameRef = [CLCoreText getFrameRef:pageModel.showContent rect:CGRectMake(0, 0, pageModel.contentSize.width, pageModel.contentSize.height)];
}

- (void)setContent:(NSAttributedString *)content {
    _content = content;
    
    self.frameRef = [CLCoreText getFrameRef:content rect: CGRectMake(0, 0, CL_READ_VIEW_RECT().size.width, CL_READ_VIEW_RECT().size.height)];
}

- (void)setFrameRef:(CTFrameRef)frameRef {
    _frameRef = frameRef;
    if (_frameRef != NULL) {
        [self setNeedsDisplay];
    }
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
    }
    return self;
}
/// 绘制
- (void)drawRect:(CGRect)rect {
//    [super drawRect:rect];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
    CGContextTranslateCTM(ctx, 0, self.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    CTFrameDraw(self.frameRef, ctx);
}
@end
